/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.WMSCapabilities = function(config){
	
	this.geoMapPanel = new GBIF.GeoSpatial.MapComponent({
			height: 400
		,	width: 500
		,	region: 'center'
		,	border: false
		,	title: "Layer Preview"
	});
	
	this.geoGridPanel=new GBIF.GeoSpatial.WMSCapabilitiesLayers ({	
			region : 'west'
		,	width: 400
		,	split: true
		,	autoScroll: true 
		,	border: false
	});
		
	this.geoGridPanel.on('addLayer',this.loadLayer,this);
	this.geoGridPanel.on('showWin',this.showLayers,this);
	
	Ext.apply(this,config,{
			layout: 'border'
		,	height: '500'
		,	border: false
		,	tbar: [{
					xtype:'label'
				,	style:"padding:0px 5px"	
				,	text:'WMS Server URI: '	
			},{
					xtype: 'textfield'  
				,	fieldLabel: 'WMS Server URI:'
				,	width:350
				,	selectOnFocus: true
				,	ref: '../url'
			},{
					text: 'Load'
				,	style:"padding:0px 5px"
				,	handler: this.loadUrl
				, scope: this
			}]
		,	proxyUrl: "../resources/api/getcapabilities-proxy.php?url="
		,	items: [ this.geoMapPanel, this.geoGridPanel ]
	});

	GBIF.GeoSpatial.WMSCapabilities.superclass.constructor.call(this, config);
}

Ext.extend(GBIF.GeoSpatial.WMSCapabilities,Ext.Panel, {

		loadUrl: function(){
			var url = this.url.getValue();
			this.clearLayer();
			if(url!=''){
				this.geoGridPanel.store.baseParams={
					url:url
				};
				this.geoGridPanel.store.load();
			} else {
				GBIF.GeoSpatial.Notice.msg("Error","Please enter url");
			}
		}

	,	loadLayer:function(layer){
			Ext.each(layer,function(maplayer){
				var layer = maplayer.data.layer.clone();
				this.geoMapPanel.map.addLayer(layer);
			}, this);
		}

	,	showLayers: function(layerWin){
			for(var i=0;i<this.geoMapPanel.map.layers.length;i++){
				if (!this.geoMapPanel.map.layers[i].isBaseLayer) {
					this.geoMapPanel.map.removeLayer(this.geoMapPanel.map.layers[i]);
				}
			}

			Ext.each(layerWin,function(maplayer){
				var layer = maplayer.data.layer.clone();
				this.geoMapPanel.map.addLayer(layer);
			}, this);
		}
		
	,	clearLayer:function(){
			Ext.each(this.geoMapPanel.map.layers,function(maplayer){
				if(!maplayer.isBaseLayer ){
					this.geoMapPanel.map.removeLayer(maplayer);
				}
			},this);
		}

	,	clearGrid : function(){
			this.geoGridPanel.store.removeAll();
   	}

});